package com.mdd.front.factory.chatAi;

import com.mdd.common.util.RedisUtils;
import io.github.asleepyfish.config.ChatGPTProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import java.time.Duration;


/**
 * 描述: 静态工厂
 *
 * @author: XYueong
 * @date: 2023/12/6
 */
@Slf4j
public class OpenAiProxyServiceFactory {


    private static ChatBaseOpenAiProxyService openAiProxyServiceRealColumn;


    public static ChatBaseOpenAiProxyService createProxyService() {
        if (openAiProxyServiceRealColumn == null) {

            Object proxyPortObj = RedisUtils.get("config:proxyPort");
            Object keyObj = RedisUtils.get("config:openAiApiKey");
            Object proxyHostObj = RedisUtils.get("config:proxyHost");
            Object chatModelObj = RedisUtils.get("config:openAiApiChatModel");


            String key = (String) keyObj;
            String proxyHost = (String) proxyHostObj;
            Integer proxyPort = (Integer) proxyPortObj;
            String chatModel = (String) chatModelObj;

            ChatGPTProperties properties = new ChatGPTProperties();

            if (proxyHost != null && proxyPort != null) {
                properties.setProxyHost(proxyHost);
                properties.setProxyPort(proxyPort);
                log.info("代理配置成功");
            } else {
                log.info("代理配置忽略");
            }

            Assert.notNull(chatModel, "未配置语言模型");
            properties.setChatModel(chatModel);

            properties.setSessionExpirationTime(5);

            Assert.notNull(key, "未配置ChatGpt密钥");
            properties.setToken(key);
            openAiProxyServiceRealColumn = new ChatBaseOpenAiProxyService(properties, Duration.ZERO);

        }
        return openAiProxyServiceRealColumn;
    }

}
